package com.example.business.service;

import com.example.business.domain.Novel;
import com.example.business.domain.NovelChapter;
import com.example.business.mapper.NovelChapterMapper;
import com.example.business.mapper.NovelMapper;
import com.example.common.BaseEntity;
import com.example.common.PageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p> Novel </p>
 *
 * @author Chris
 * @since 2022/10/29 17:33
 */
@Service
public class NovelService {

    private final NovelChapterMapper novelChapterMapper;
    private final NovelMapper novelMapper;

    public NovelService(NovelMapper novelMapper, NovelChapterMapper novelChapterMapper) {
        this.novelMapper = novelMapper;
        this.novelChapterMapper = novelChapterMapper;
    }

    public PageVo<?> listNovel(Novel novel, BaseEntity baseEntity) {
        PageHelper.startPage(baseEntity.getPageNum(), baseEntity.getPageSize());
        final List<Novel> novels = novelMapper.listNovels(novel);
        final PageInfo<Novel> pageInfo = new PageInfo<>(novels);
        PageHelper.clearPage();
        return new PageVo<>(novels, pageInfo.getTotal(), (long) baseEntity.getPageNum(), (long) baseEntity.getPageSize());
    }


    public void save(Novel novel) {
        novelMapper.save(novel);
    }

    public PageVo<?> listNovelChapter(Novel novel, BaseEntity baseEntity) {
        PageHelper.startPage(baseEntity.getPageNum(), baseEntity.getPageSize());
        final List<NovelChapter> novelChapters = novelChapterMapper.listNovelChapters(novel);
        final PageInfo<NovelChapter> pageInfo = new PageInfo<>(novelChapters);
        PageHelper.clearPage();
        return new PageVo<>(novelChapters, pageInfo.getTotal(), (long) baseEntity.getPageNum(), (long) baseEntity.getPageSize());
    }
}